package com.jiutong.meipengdai.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.entity.HouseOrRecordBean;
import com.jiutong.meipengdai.entity.ToApplyLoanRespond;
import com.jiutong.meipengdai.http.model.interfaces.OnItemClickListener;
import com.jiutong.meipengdai.utils.NumUilt;

import java.util.List;

/**
 * Created by suxi on 2017/6/4.
 */

public class HouseGalleryAdapter extends RecyclerView.Adapter<HouseGalleryAdapter.ViewHolder> {
    private final Context mContext;
    private LayoutInflater mInflater;
    private List<HouseOrRecordBean> mDatas;
    private OnItemClickListener mListener;

    public HouseGalleryAdapter(Context context, List<HouseOrRecordBean> datats) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        mDatas = datats;
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.item_loan_house_card,
                viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.ll_card = (LinearLayout) view.findViewById(R.id.ll_card);
        viewHolder.houseNum = (TextView) view.findViewById(R.id.tv_housenum);
        viewHolder.houseName = (TextView) view.findViewById(R.id.tv_housename);
        viewHolder.money = (TextView) view.findViewById(R.id.tv_houseprice);
//        viewHolder.dollarIcon = (TextView) view.findViewById(R.id.tv_dollar_icon);
        viewHolder.unit = (TextView) view.findViewById(R.id.tv_unit);
        viewHolder.desc = (TextView) view.findViewById(R.id.tv_desc);
        return viewHolder;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
        viewHolder.ll_card.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onItemClick(v, position);
            }
        });
        if (mDatas.get(position).isSelected()) {
            viewHolder.ll_card.setBackgroundResource(R.mipmap.loan_bill_selected);
            viewHolder.houseNum.setTextColor(mContext.getResources().getColor(R.color.light_orange));
            viewHolder.houseName.setTextColor(mContext.getResources().getColor(R.color.light_orange));
//            viewHolder.dollarIcon.setTextColor(mContext.getColor(R.color.light_orange));
            viewHolder.unit.setTextColor(mContext.getResources().getColor(R.color.light_orange));
            viewHolder.desc.setTextColor(mContext.getResources().getColor(R.color.light_orange));
            viewHolder.money.setTextColor(mContext.getResources().getColor(R.color.orange));

        } else {
            viewHolder.ll_card.setBackgroundResource(R.mipmap.loan_bill_noselected);
            viewHolder.houseNum.setTextColor(mContext.getResources().getColor(R.color.gray));
            viewHolder.houseName.setTextColor(mContext.getResources().getColor(R.color.gray));
//            viewHolder.dollarIcon.setTextColor(mContext.getColor(R.color.gray));
            viewHolder.unit.setTextColor(mContext.getResources().getColor(R.color.gray));
            viewHolder.desc.setTextColor(mContext.getResources().getColor(R.color.gray));
            viewHolder.money.setTextColor(mContext.getResources().getColor(R.color.gray));
        }
        viewHolder.houseNum.setText(mDatas.get(position).getHouseNo());
        viewHolder.houseName.setText(mDatas.get(position).getHouseAddress());

        float amount = Float.parseFloat(mDatas.get(position).getFainalAssAmount());
        if (amount >= 10000) {
            viewHolder.money.setText(NumUilt.formatFloat(amount / 10000) + "");
            viewHolder.unit.setVisibility(View.VISIBLE);
        } else {
            viewHolder.money.setText(NumUilt.formatFloat(amount) + "");
            viewHolder.unit.setVisibility(View.GONE);
        }
    }

    public void setOnItemClick(OnItemClickListener listener) {
        this.mListener = listener;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        LinearLayout ll_card;
        TextView houseNum;
        TextView houseName;
        TextView money;
        TextView unit;
        TextView desc;

        public ViewHolder(View arg0) {
            super(arg0);
        }
    }
}
